// JavaScript Document
window.onload = function(){
	ml.addEvent();
	//日期插件
	var mrd = document.getElementById("mrdate");
	d().addDateTo(mrd);
	
	//登录页面后通过myajax获取进一步的信息
	var myajax = getByAjax();
	myajax.init(null,"get",true);
	myajax.get("/networkforum/user/user_main.do",showInfo);
	myajax = null;
	
	scrollEvent("/networkforum/user/load_my_notice.do","dongtai");

}
function scrollEvent(url,moduleName){
	//添加scroll事件
	var param = {
		url:url,
		size:20,
		callBack:activeInfo().addNode,
		target:window,
		data:{moduleName:moduleName},
		containerId:"mcinfo"
	}
	page.scrolls(param);	
}
function getHotInfo(){
	var myajax = getByAjax();
	myajax.init(null,"get",true);
	myajax.get("/networkforum/user/hot_info.do",hotInfoBack);
	myajax = null;
}
/*******************通过myajax异步获取数据后的回调function***********************/
function showInfo(d){
	if(d ==null){
		alert("数据加载失败，请重新尝试！");
	}
//	var data = eval("("+d[0]+")");
	var data = toJson(d);
	//属性说明：
	//userBase为用户基本信息
	//userActive为用户动态
	//userFriends为用户好友
	//visitHistory为访问记录
	//friendNews为好友状态
	var ub = data.userBase;
	var userActive = data.userActive;
	var userFriends = data.userFriends;
	var visitHistory = data.visitHistory;
	var centerInfo = data.activeInfo;
	//用户基本信息展示
	userBase().show(ub);
	//用户动态数量提示信息
	userNotice().show(userActive);
	//展示用户好友信息
	friend().show(userFriends);
	//展示访问记录
	//visitHistory().show(vistiHistory);
	visitorInfo(visitHistory);
	//好友动态
	//activeInfo(document.getElementById("mcinfo"),centerInfo).addNode();	
	activeInfo().addNode(centerInfo);
	//获取第二步数据
	getHotInfo();
	
	//好友分页
	var myajax = getByAjax();
	myajax.init(null,"get",true);
	myajax.get("/networkforum/friend/count_size.do",function(d){
		paging.page({
			size:10,
			total:Math.ceil(d/10),
			url:"/networkforum/friend/friend_list.do",
			previou:document.getElementById("previouPage"),
			next:document.getElementById("nextPage"),
			current:document.getElementById("currentPage"),
			max:document.getElementById("totalPage"),
			callBack:friend().show,
		});
		/*paging.page({
			index:0,
			size:10,
			total:Math.ceil(d/10),
			url:"/networkforum/friend/friend_list.do",
			previou:document.getElementById("previouPage"),
			next:document.getElementById("nextPage"),
			current:document.getElementById("currentPage"),
			callBack:friend().show,
		});*/
	});
	myajax = null;
	
	systemStyle(data.ss,"动态");
}
function visitorInfo(data){
	var vp = {
		userPhoto:{
			param:{id:"userPhoto"},
			bind:{userId:"userId"}
		},
		userName:{
			param:{id:"userName"},
		},
		visitTime:{
			param:{id:"visitTime"}
		}
	}
	//	page.view('articleInner',article,articleOption);
	page.view("visitInner",data,vp);
	vp = null;
}
/****************左侧消息提示*******************/
var userNotice = function(){
	var un = function(){
		//var friendsNotice;
		//var careNotice;
		var mn;
		var an;
		var dn;
		var pn;
		var init = function(){
			cn = document.getElementById("care");
			mn = document.getElementById("messages");
			an = document.getElementById("article");
			dn = document.getElementById("discuss");
			pn = document.getElementById("picture");
			fn = document.getElementById("friendApply");
		}
		this.show = function(data){
			init();
			//cn.innerHTML = data.careNotice;
			mn.innerHTML = data.messageNotice;
			an.innerHTML = data.articleNotice;
			dn.innerHTML = data.discussNotice;
			pn.innerHTML = data.pictureNotice;
			fn.innerHTML = data.applyNotice;
		}
	}
	return new un();
}

/*****************基本信息绑定*********************/
var userBase = function(){
	var ub = function(){	
		var user_picture;
		var user_name;
		//var user_care;
		var user_friends;
		var user_score;
		var init = function(){
			user_picture = document.getElementById("user_picture");
			user_name = document.getElementById("user_name");
			//user_care = document.getElementById("user_care");
			user_friends = document.getElementById("user_friends");
			user_score = document.getElementById("user_score");
		}
		this.show = function(data){
			//对象初始化
			init();
			user_picture.src = data.userPicture;
			user_picture.module = "user";
			user_picture.targetId = data.userId;
			user_picture.title = data.userSex;
			user_name.innerHTML = data.userName;
			user_name.title = data.userSex;
			//user_care.innerHTML = "关注("+data.userCare+")";
			user_friends.innerHTML = "好友 "+data.userFriends+"";
			user_score.innerHTML = "积分 "+data.userScore+"";
		}
	}
	return new ub();
}

/********************用户好友信息展示**************************/
var friend = function(){
	var f = function(){
		//该数据格式需满足[{"image":"","name":"","news":""}]
		var data;
		var f ;
		var frag;
		this.show = function(d){
			data = d;
			//f = document.createElement("section");
			f = document.getElementById("friendInner");
			f.innerHTML = "";
			//f.style.cssText = "width:100%;min-height:265px;height:auto,overflow:hidden";
			frag = document.createDocumentFragment();
			if(data instanceof Array&&data.length>0){
				for(var i =0;i<data.length;i++){
					insert(data[i]);	
				}
				//document.getElementById("mlul_2").appendChild(f);
				f.appendChild(frag);
			}
			else{
				var h2 = document.createElement("h2");
				h2.innerHTML = "无任何好友信息！";
				h2.style.cssText = "font-size:12px;";
				f.appendChild(h2);
				//document.getElementById("mlul_2").appendChild(f);				
			}
			//addBottom();
		}
		var insert = function(d){
			var node = getTag("section");
			var image = getTag("img");
			var p = getTag("p");
			node.style.cssText = "padding:5px 0px;width:95%;height:25px;display:inline-block";
			image.style.cssText = "margin-right:10px;width:25px;height:25px;float:left;display:inline";
			p.style.cssText = "display:inline;float:right;width:75%;height:25px;line-height:25px;vertical-align:middle;text-align:right";
			
			image.src = d.image;
			image.module="user";
			image.targetId = d.userId;
			
			p.innerHTML = d.name+" "+d.news;
			
			node.appendChild(image);
			node.appendChild(p);
			//f.appendChild(node);
			frag.appendChild(node);
		}
		var addBottom = function(){
			var node = document.createElement("section");
			node.style.cssText = "margin-top:10px;height:20px;width:80%;";
			node.innerHTML = "<a style='float:left' href = '#'>上一页</a><p style='margin:0px 15px 0px 15px;float:left'>1/6</p><a style='float:left' href='#'>下一页</a>";
			document.getElementById("mlul_2").appendChild(node);	
		}
		var getTag = function(tagName){
			return document.createElement(tagName);
		}
	}	
	return new f();
}

/********************用户访问记录信息展示**********************/
var visitHistory = function(){
	var h = function(){
		var frame;	
		var arr ;
		//该处的传入数组参数必须严格按照规定的json格式，否则会出现展示错位
		//[{"image":"image","name":"name","time":"time"}]
		this.show = function(a){
			arr = a;
			frame = document.getElementById("mrvisit");		
			addHeader();
			if(arr != null && arr.length>0)
				addTable();
			else{
				var section = document.createElement("section");
				section.style.cssText = "height:80%;width:100%;";
				var h3 = document.createElement("h3");
				h3.innerHTML = "暂无访问记录!";
				h3.style.cssText = "font:12px normal;color:#6e5b4d;";
				section.appendChild(h3);
				frame.appendChild(section);
			}
			addBottom();
		}
		var addHeader = function(){
			var header = document.createElement("header");
			header.style.cssText = "width:100%;height:25px;padding:10px 0px 0px 5px;";
			header.innerHTML = 
			"<ul style='color:#6e5b4d;'><li style='margin-right:10px;font-size:12px;'>谁看过我</li><li style='font-size:12px;'>我看过谁</li></ul>";
			frame.appendChild(header);
		}
		var addTable = function(){
			var t = document.createElement("table");
			t.style.cssText = "width:98%;height:80%;margin:3px;border-collapse:collapse;";
			var index = 0;
			var tbody = document.createElement("TBODY");
			for(var i =0;i<3;i++){
				var tr = document.createElement("TR");
				tr.style.cssText = "height:95px;width:50px;overflow:hidden;";
				for(var j =0;j<3;j++){
					var td = document.createElement("TD");
					td.style.cssText = "display:inline-table;height:95px;width:75px;overflow:hidden;";
					if(index<arr.length){
						addInfo(td,arr[index++]);
					}
					tr.appendChild(td);
				}
				tbody.appendChild(tr);					
			}
			t.appendChild(tbody);
			frame.appendChild(t);
		}
		var addInfo = function(node,content){
			var f = document.createElement("section");
			f.style.cssText = "height:100%;width:100%;float:left;";
			var image = document.createElement("img");
			var name = document.createElement("h2");
			var time = document.createElement("h4");
			image.module = "user";
			image.targetId = content.userId;
			image.src = content.image;
			name.innerHTML = "<a style='color:#ffac50;' href = '#'>"+content.name+"</a>";
			time.innerHTML = content.time;
			image.style.cssText = "position:relative;width:55px;height:55px;float:left;";
			name.style.cssText = "float:left;position:relative;font-weight:normal;text-align:left; width:100%;height:20px;";
			time.style.cssText = ";font-weight:normal;color:#999;text-align:left;width:100%;height:20px;";
			f.appendChild(image);
			f.appendChild(name);
			f.appendChild(time);
			node.appendChild(f);
		}
		var addBottom = function(){
			var f = document.createElement("section");
			f.style.cssText = "margin-top:10px;height:20px;width:60%;";
			f.innerHTML = "<a style='float:left' href = '#'>上一页</a><p style='margin:0px 15px 0px 15px;float:left'>1/6</p><a style='float:left' href='#'>下一页</a>";
			frame.appendChild(f);	
		}
	}
	return new h();
}

/************************mcinfo 数据展示*********************/
/************************展示JS水平的代码*********************/

/**********************左侧导航链接对象*********************/
var ml = (function(){
	function mlul1(){
		var index = 0;
		this.getIndex = function(){
			return index;
		}
		this.setIndex = function(i){
			index = i;
		}	
	}
	mlul1.prototype.addEvent = function(){
		var ul = document.getElementById("mlul_1");
		for(var i=0;i<ul.children.length;i++){
			var ul_li = ul.children[i];
			/*(function(i){
				ul_li.onclick = function(){
					//ml.clickEvent(i)
				}	
			})(i);*/
			if(this.getIndex() != i){			
				if(window.ActiveXObject){
					(function(ul_li){
						ul_li.attachEvent("onmouseover",function tt(){
							ml.mlulEvent(event,ul_li);	
						});	
						ul_li.attachEvent("onmouseout",function tt(){
							ml.mlulEvent(event,ul_li);	
						});	
					})(ul_li);
				}
				else{
					ul_li.addEventListener("mouseover",this.mlulEvent,true);	
					ul_li.addEventListener("mouseout",this.mlulEvent,true);	
				}
			}
		}
	}
	mlul1.prototype.mlulEvent = function(event,obj){
		var e = window.event||event; 
		if(e.type == "mouseover"){
			changeBackColor(obj||this,"#f8dba3");
		}
		else if(e.type == "mouseout"){
			changeBackColor(obj||this,"#fcecd3");
		}
	}
	return new mlul1();
})();

function userActive(){
	var info = document.getElementById("useractive");
	var active = info.value;
	
	//var url = "/networkforum/user/user_active.do?userActive="+encodeURIComponent(active);;
	var url = "/networkforum/user/user_active.do?userActive="+encodeURIComponent(active);;
	var myajax = getByAjax();
	myajax.init(null,"post",true);
	myajax.get(url,userActiveReturn);
	info.value = "";
}

function userActiveReturn(d){
	var arr = new Array();
	//for(var i =0;i<d.length;i++){
//		arr.push(eval("("+d[i]+")"));
		arr.push(toJson(d));
	//}
	activeInfo().insertBefore(arr);
}
//右侧热门信息展示
function hotInfoBack(d){
//	var data = eval("("+d+")");
	var data = toJson(d);
	
	hotUser(data.hotUser);
	hotArticle(data.hotArticle);
	hotPicture(data.hotPicture);
	var images = document.getElementsByTagName("img");
	for(var i =0;i<images.length;i++){
		if(images[i].targetId){
			BindOnclickEvent(images[i]);
		}
	}
}

//热门博主信息展示
function hotUser(d){
	var ul = document.getElementById("hotUserInner");
	if(d.length == 0){
		var li = document.createElement("li");
		li.innerHTML = "还无突出博主！";
		ul.appendChild(li);
		return;
	}
	var frag = document.createDocumentFragment();
	for(var i =0;i<d.length;i++){
		var data = d[i];
		var li = document.createElement("li");
		var img = document.createElement("img");
		var label = document.createElement("label");
		li.title = "积分"+data.userScore;
		img.module = "user";
		img.targetId = data.userId;
		img.src = data.userPhoto;
		label.innerHTML = data.userName;
		li.appendChild(img);
		li.appendChild(label)
		frag.appendChild(li);
	}
	ul.appendChild(frag);
}
//热门博客
function hotArticle(d){
	var ul = document.getElementById("hotArticleInner");
	if(d.length == 0){
		var li = document.createElement("li");
		li.innerHTML = "还无突出博主！";
		ul.appendChild(li);
		return;
	}
	var frag = document.createDocumentFragment();
	for(var i =0;i<d.length;i++){
		var data = d[i];
		var li = document.createElement("li");
		var label = document.createElement("label");
		li.articleId = data.articleId;
		li.title = "来源于 "+data.userName;
		label.innerHTML = (i+1)+"楼:"+data.articleName;
		li.appendChild(label);
		
		frag.appendChild(li);
		li.onclick = function(event){
			alert("完善中！");
		}
	}
	ul.appendChild(frag);
}
//热门照片信息展示
function hotPicture(d){
	var ul = document.getElementById("hotPictureInner");
	if(d.length == 0){
		var li = document.createElement("li");
		li.innerHTML = "还无突出博主！";
		ul.appendChild(li);
		return;
	}
	var frag = document.createDocumentFragment();
	for(var i =0;i<d.length;i++){
		var data = d[i];
		var li = document.createElement("li");
		var img = document.createElement("img");
		var label = document.createElement("label");
		li.title = "评论次数 "+data.reviewTimes;
		img.targetId = data.pictureId;
		img.module = "picture";
		img.src = data.pictureSrc;
		label.innerHTML = "来源于"+data.userName+" "+data.uploadTime;
		
		li.appendChild(img);
		li.appendChild(label)
		frag.appendChild(li);
	}
	ul.appendChild(frag);
}